package com.panda.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.panda.takeaway.dao.BatchMapper;
import com.panda.takeaway.dao.ShopMapper;
import com.panda.takeaway.entity.dto.CommonResult;
import com.panda.takeaway.entity.po.Batch;
import com.panda.takeaway.entity.vo.BatchVo;
import com.panda.takeaway.service.BatchService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author John
 */
@Service
public class BatchServiceImpl implements BatchService {

    @Resource
    private BatchMapper batchMapper;
    @Resource
    private ShopMapper shopMapper;


    @Override
    public CommonResult<?> setBatch(BatchVo batchVo) {


        Assert.notNull(batchVo, "batch对象为null");

        if (shopMapper.selectById(batchVo.getShopId()) == null) {
            return CommonResult.operateFailWithMessage("该店铺不存在");
        }
        //判断是否存在相同批次
        QueryWrapper<Batch> wrapper = new QueryWrapper<>();
        wrapper.eq("batch", batchVo.getBatch());
        if (batchMapper.selectOne(wrapper) != null) {

            return CommonResult.operateFailWithMessage("该批次已存在，不可设置相同批次");

        }
//        在这里应该还要对时间进行一些判断，例如结束时间要晚于开始时间，
//         且应该设置最小的间隔，然后配送时间也应该晚于结束时间
        Batch batch = new Batch();
        BeanUtils.copyProperties(batchVo, batch);
//        id自动生成
        batch.setId(null);

        if (batchMapper.insert(batch) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage("系统繁忙，请稍后重试！");
    }


    @Override
    public CommonResult<?> queryBatch(Long shopId) {

        QueryWrapper<Batch> wrapper = new QueryWrapper<>();
        wrapper.eq("shop_id", shopId)
                .orderByAsc("batch");

        List<Batch> batchList = batchMapper.selectList(wrapper);
        if (batchList.size() == 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateSuccess(batchList);
    }


    @Override
    public CommonResult<?> updateBatch(BatchVo batchVo) {

        System.out.println(batchVo);
        Batch batchCheck = batchMapper.selectById(batchVo.getBatchId());

        if (batchCheck == null) {
            return CommonResult.operateFailWithMessage("该批次不存在");
        }

        //判断是否修改批次，如果修改则判断修改后的批次是否已存在
        if (!(batchCheck.getBatch().equals(batchVo.getBatch()))) {
            QueryWrapper<Batch> wrapper = new QueryWrapper<>();
            wrapper.eq("batch", batchVo.getBatch())
                    .ne("id", batchCheck.getId());
            if (batchMapper.selectOne(wrapper) != null) {
                return CommonResult.operateFailWithMessage("该批次已存在，不可设置相同批次");
            }
        }

        Batch batch = new Batch();
        BeanUtils.copyProperties(batchVo, batch);
//        设置id
        batch.setId(batchVo.getBatchId());
        //避免店铺id修改
        batch.setShopId(null);

        if (batchMapper.updateById(batch) > 0) {
            return CommonResult.operateSuccess();
        }
        return CommonResult.operateFailWithMessage("系统繁忙，修改批次信息失败！");
    }
}

